<template>
  <ych-modal
    :fullscreen="dialogSize === 'default' || dialogSize === 'full'"
    :size="dialogSize"
    :title="title"
    :visible="visible"
    :bodyStyle="bodyStyle"
    :destroyOnClose="true"
    @ok="handleOk"
    :footer="footer"
    @cancel="handleCancel">
    <form-redirect v-if="formType === 'form'"
                   ref="formRedirect"
                   :func="func"
                   :formId="formId"
                   :formCode="formCode"
                   :fks="fks"
                   :parentValue="parentValue"
                   :saveInPage="saveInPage"
                   :disQueryDB="disQueryDB"
                   :record="record"
                   :stores="stores"
                   :hiddenSaveBut="true"
                   @close="close"
                   @hideDialogButtons="hideDialogButtons"
                   @addParentSpinning="addSpinning"
                   @delParentSpinning="delSpinning"
                   @refreshParent="refreshParent"
                   @setFormTitle="setFormTitle"
                   @setEnableDraft="setEnableDraft">
    </form-redirect>
    <view-layout-redirect v-else-if="formType === 'view'"
                          ref="viewRedirect"
                          :view-code="formCode"
                          :stores="stores"
                          :formOption="formOption"
                          :parameterValue="parameterValue"
                          @closeFromView="close"
                          @hideDialogButtonsFromView="hideDialogButtons"
                          @addParentSpinningFromView="addSpinning"
                          @delParentSpinningFromView="delSpinning"
                          @refreshParent="refreshParent"
                          @handleOkFromView="handleOk"
                          @setFormTitleFromView="setFormTitle"
                          @setEnableDraft="setEnableDraft">
    </view-layout-redirect>

    <template slot="footer">
      <a-button key="back" @click="handleCancel" :disabled="spinning > 0">{{ $t('m.common.button.close') }}</a-button>
      <a-button key="saveDraft" type="primary" @click="saveDraft" v-if="!readOnly && enableDraft" :disabled="spinning > 0">{{ $t('m.common.button.saveDraft') }}</a-button>
      <a-button key="submit" type="primary" @click="handleOk" v-if="!readOnly" :disabled="spinning > 0">{{ $t('m.common.button.save') }}</a-button>
    </template>
  </ych-modal>
</template>

<script>

  import FormRedirect from './FormRedirect'
  import config from '@/settings'

  export default {
    name: 'FormRedirectModal',
    components: {
      FormRedirect,
      ViewLayoutRedirect: () => import('./ViewLayoutRedirect')
    },
    props: {
      // 仅在页面暂时存储，用于子表添加
      saveInPage: {
        type: Boolean,
        default: false
      },
      stores: {
        type: Object,
        default: () => {
          return {}
        }
      }
    },
    computed: {
      formOption() {
        return {
          func: this.func,
          formId: this.formId,
          fks: this.fks,
          parentValue: this.parentValue,
          saveInPage: this.saveInPage,
          disQueryDB: this.disQueryDB,
          record: this.record,
          isTab: true,
        }
      },
      bodyStyle() {
        if (this.formType === 'view') {
          return {
            padding: 0,
            margin: 0,
            background: "#F0F2F5"
          }
        }
        return {}
      },
    },
    data () {
      return {
        title: this.$t('m.eform.formDesign.label.biao-dan-ye-mian'),
        visible: false,
        func: null,
        formId: null,
        formCode: null,
        fks: null,
        parentValue: {},
        readOnly: false,
        footer: undefined,
        dialogSize: 'default',
        spinning: 0,
        disQueryDB: false, // 不查询数据库
        record: null, // 页面对象
        formType: 'form',
        parameterValue: [],
        enableDraft:false,
        needClose: false,
      }
    },
    methods: {
      //设置标题
      setFormTitle(title) {
        let titleName = title || this.$t('m.eform.formDesign.label.biao-dan')
        let funcName = this.$t('m.common.button.add')
        if (this.func === 'edit') {
          funcName = this.$t('m.common.button.update')
        } else if (this.func === 'detail') {
          funcName = this.$t('m.common.title.detail')
        }
        if(config.formTitleContainFormName){
          this.title = funcName + " " + titleName
        }else{
          this.title = funcName
        }

      },
      handleCancel () {
        if(this.spinning > 0){
          this.needClose = true
          return;
        }
        this.visible = false
      },
      handleOk($vue,isDraft) {
        const that = this;
        this.addSpinning();
        if (this.formType === 'form') {
          this.$refs.formRedirect.save(this.afterSave)
        } else {
          $vue.save(this.afterSave,null,isDraft)
        }
        setTimeout(function () {
          that.delSpinning();
        }, 500)
      },
      saveDraft(){
        const that = this;
        this.addSpinning();
        this.$refs.formRedirect.save(this.afterSave,null,true)
        setTimeout(function () {
          that.delSpinning();
        }, 500)
      },
      afterSave(id,saveSucessMsg) {
        if(saveSucessMsg){
          this.messageUtil.success(saveSucessMsg)
        }else{
          this.messageUtil.success(this.$t('m.eform.formDesign.label.bao-cun-cheng-gong'))
        }
        // view方式时，不关闭当前页面
        // 普通模式（form）时，关闭当前页面
        // 后续改为可配置参数
        if (this.formType === 'form') {
          this.visible = false
        }
      },
      hideDialogButtons(){
        this.setFooter(null)
      },
      //关闭页面
      close () {
        this.handleCancel()
      },
      //刷新父页面
      refreshParent (id, tableName, formData) {
        this.$emit('refreshParent', id, tableName, formData)
      },
      setFooter(value) {
        this.footer = this.formType === 'view' ? null : value
      },
      add (formCode, fks, parentValue, dialogSize) {
        this.addForm({
          formCode: formCode,
          fks: fks,
          parentValue: parentValue,
          dialogSize: dialogSize,
        })
      },
      addForm(options){
        this.formType = options.formType || 'form'
        this.dialogSize = options.dialogSize || 'default'
        this.disQueryDB = false
        this.record = null
        this.func = 'add'
        this.formId = undefined
        this.formCode = options.formCode
        this.fks = options.fks
        this.parentValue = options.parentValue || {}
        this.parameterValue = options.parameterValue || []
        this.title = this.$t('m.common.button.add')
        this.readOnly = false
        this.enableDraft = false
        this.visible = true
        this.needClose = false
        this.setFooter(undefined)
        this.spinning = 0;
      },
      edit (id, formCode, dialogSize, disQueryDB, record) {
        this.editForm({
          id: id,
          formCode: formCode,
          dialogSize: dialogSize,
          disQueryDB: disQueryDB,
          record: record,
        })
      },
      editForm(options){
        this.formType = options.formType || 'form'
        this.dialogSize = options.dialogSize || 'default'
        this.disQueryDB = options.disQueryDB;
        this.record = options.record;
        this.func = 'edit'
        this.formId = options.id
        this.formCode = options.formCode
        this.fks = null
        this.parentValue = {}
        this.parameterValue = options.parameterValue || []
        this.title = this.$t('m.common.button.update')
        this.readOnly = false
        this.enableDraft = false
        this.visible = true
        this.needClose = false
        this.setFooter(undefined)
        this.spinning = 0;
      },
      detail (id, formCode, dialogSize, disQueryDB, record) {
        this.detailForm({
          id: id,
          formCode: formCode,
          dialogSize: dialogSize,
          disQueryDB: disQueryDB,
          record: record,
        })
      },
      detailForm(options){
        this.formType = options.formType || 'form'
        this.dialogSize = options.dialogSize || 'default'
        this.disQueryDB = options.disQueryDB;
        this.record = options.record;
        this.func = 'detail'
        this.formId = options.id
        this.formCode = options.formCode
        this.fks = null
        this.parentValue = {}
        this.parameterValue = options.parameterValue || []
        // 纯列表页面不需要标题，表单页面会设置标题
        this.title = this.$t('m.common.title.detail')
        this.readOnly = true
        this.visible = true
        this.needClose = false
        this.setFooter(undefined)
        this.spinning = 0;
      },
      addSpinning(){
        this.spinning ++
      },
      delSpinning(){
        this.spinning --
        if (this.spinning === 0 && this.needClose) {
          this.needClose = false
          this.handleCancel()
        }
      },
      setEnableDraft(enableDraft){
        this.enableDraft = enableDraft
      }
    }
  }
</script>
